-
Notifications
You must be signed in to change notification settings - Fork 12
Exercise 2 #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Exercise 2 #5
Conversation
TODO("Implement me!!") | ||
|
||
|
||
// for ( i in 0 ..this.size - 1){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please clean up all the comments before pushing the changes and creating the PR.
@@ -20,7 +20,10 @@ import org.jetbrains.exercise2.task3.findPairWithBiggestDifference | |||
*/ | |||
|
|||
internal fun List<Int>.findHighestSumPairFunctional(): Pair<Int, Int> { | |||
TODO("Implement me!!") | |||
// var par = Pair<Int, Int>(0, 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please clean up all the comments before pushing the changes and creating the PR.
|
||
return resultPair!! | ||
// var resultPair: Pair<Int, Int>? = null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please clean up all the comments before pushing the changes and creating the PR.
} | ||
|
||
internal fun List<Country>.findLanguageSpokenInMostCountries(): String { | ||
TODO("Implement me!!!") | ||
val jezik = this.flatMap { it.languages }.groupingBy { it }.eachCount().maxBy { it.value}.key | ||
// println("Ovo je nesto: " + jezik) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please clean up all the comments before pushing the changes and creating the PR.
} | ||
|
||
internal fun List<Country>.filterCountriesThatSpeakLanguage(language: String): List<Country> { | ||
TODO("Implement me!!!") | ||
|
||
// var lista = listOf<Country>(this.first()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please clean up all the comments before pushing the changes and creating the PR.
} | ||
|
||
internal fun List<Country>.findLanguageSpokenInMostCountries(): String { | ||
TODO("Implement me!!!") | ||
val jezik = this.flatMap { it.languages }.groupingBy { it }.eachCount().maxBy { it.value}.key |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please declare variable names in English.
var first = this[0] | ||
var second = this[0] | ||
for (i in 0 ..< this.size -1){ | ||
if (this[i+1] >= first ){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are accessing this[i + 1]
many times, hence we assign this to variable nextNum
and use nextNum
variable everywhere else. This will improve readability.
second = this[i+1] | ||
} | ||
} | ||
return Pair<Int, Int>(first, second) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The <Int, Int>
can be omitted since the compiler is smart enough to infer the generic type.
Also, there is an alternative way to declare a pair, and that is by using to
infix extension function:
first to second
// Prolazak kroz jednu petlju | ||
var first = this[0] | ||
var second = this[0] | ||
for (i in 0 ..< this.size -1){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we run your implementation for the input listOf(3, 1, 2)
, we would get the incorrect result Pair(3, 3)
.
Are the tests green for you?
nit: this.size -1
can be written as this.lastIndex
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test data for this method seems incomplete, and your implementation has a false positive in the tests, meaning the tests are green, but the actual implementation is incorrect.
In the above example for the input listOf(3, 1, 2)
the expected output should be Pair(3, 2)
, and you implementation returns Pair(3, 3)
which is incorrect. This is because the test didn't cover the case where the highest number in the list is at the first index.
I'm very sorry for the inconvenience with incomplete test data.
Please add a missing case to the test class and fix the bug in your implementation.
// } | ||
// return resultPair!! | ||
|
||
return this.sorted().let { it.first() to it[lastIndex] } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a mistake in the implementation where the lastIndex
is stated. In the code you wrote, the lastIndex
is the last index of the receiver object list. It's the same as if we wrote this.lastIndex
.
TLDR: you should use it.lastIndex
instead of this.lastIndex
In this case, the result will be correct, but if we, for example, filter elements of the list after sorting it, the lastIndex
of the list we are operating on would change, and you would get an IndexOutOfBounds
error.
Let's show this visually. In the following example, we have the list [3, 2, 4, 5, 1]
.
After calling sorted()
on the list, we would get a new list [1, 2, 3, 4, 5]
, with the last index value being 4
.
If after that, on the result list we call filter { it > 2 }
, the new result list would be [3, 4, 5]
, where now the value of the last index is 2
, and if we would call it[this.lastIndex]
as in your code, we would get IndexOutOfBoundsException
because this.lastIndex
value is 4, which is greater than current last index.
9db668d
to
189c667
Compare
6106c56
to
8296ece
Compare
No description provided.